package com.his.struts.action;

import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.comment.CommentDao;
import com.his.struts.dao.comment.CommentImp;
import com.his.struts.form.CommentForm;
import com.his.struts.util.AjaxUtil;
import com.his.struts.util.PageUtil;
import com.his.struts.util.TranCharset;

/** 
 * MyEclipse 肖伟亮
 * Creation date: 05-31-2011
 * 
 * 
 */
public class CommentAction extends DispatchAction {
	 
	Logger log = Logger.getLogger(CommentAction.class);
	CommentDao dao = new CommentImp();
	/** 
	 * Method goComment 去点评
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 查看所有点评	
	 */
	public ActionForward goComment(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 取出医生名字
		String doctorName = request.getParameter("doctorName");
		try {
			/*
			 * 如果医生名字存在 就转换格式
			 * 否则就跳到登陆页
			 */
			if(doctorName == null){
				return mapping.findForward("login");
			} else {
				doctorName = new String(doctorName.getBytes("ISO-8859-1"),"UTF-8");
			}
		} catch (UnsupportedEncodingException e) {
			log.error(e.getMessage());
		}
		String sql = "comment where doctorName = '" + doctorName +"' order by c_id desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 2, request);
		// 如果数据不为空 
		if(rs != null){
			// 封装
			List<CommentForm> comment = dao.getBean(rs);
			// 数据放到request中
			request.setAttribute("comments", comment);
		}
		request.setAttribute("doctorName", doctorName);
		
		/*
		 * 后台
		 */
		String admin = request.getParameter("admin");
		if(admin != null){
			return mapping.findForward("admin_allComment");
		} 
		return mapping.findForward("goComment");
	}
	
	/** 
	 * Method comment 去点评
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 查看所有点评	
	 */
	public ActionForward comment(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		CommentForm commentForm = (CommentForm) form; 
		
		// 得到session中的token
		String token = (String)request.getSession().getAttribute("token");
		// 得到网页中的token
		String newToken = request.getParameter("token");
		// 是否重复提交
		if(newToken != null && newToken.equals(token)){
		} else {
			// 添加点评
			boolean ok = dao.addComment(commentForm);
		}
		// 把最新token设置到session中
		request.getSession().setAttribute("token", newToken);
		
		
		// 取出医生名字
		String doctorName = commentForm.getDoctorName();
		String sql = "comment where doctorName = '" + doctorName +"' order by c_id desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 2, request);
		// 如果数据不为空 
		if(rs != null){
			// 封装
			List<CommentForm> comment = dao.getBean(rs);
			// 数据放到request中
			request.setAttribute("comments", comment);
		}
		request.setAttribute("doctorName", doctorName);
		return mapping.findForward("goComment");
	}
	
	/** 
	 * Method query 查看该医生所有点评
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 查看所有点评	
	 */
	public ActionForward query(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		CommentForm commentForm = (CommentForm) form; 
		// 取出医生名字
		String doctorName = commentForm.getDoctorName();
		/*
		 * 否则就跳到登陆页
		 */
		if(doctorName == null){
			return mapping.findForward("login");
		}
		doctorName = TranCharset.TranEncodeTOUTF_8(doctorName);
		String sql = "comment where doctorName = '" + doctorName +"' order by c_id desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		// 如果数据不为空 
		if(rs != null){
			// 封装
			List<CommentForm> comment = dao.getBean(rs);
			// 数据放到request中
			request.setAttribute("comments", comment);
		}
		
		request.setAttribute("doctorName", doctorName);
		
		/*
		 * 后台
		 */
		String admin = request.getParameter("admin");
		if(admin != null){
			return mapping.findForward("admin_allComment");
		} 
		return mapping.findForward("goComment");
	}
	
	/** 
	 * Method addSupport 添加支持
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward null	
	 */
	public ActionForward addSupport(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		CommentForm commentForm = (CommentForm) form; 
		// 得到点评的id 
		int c_id = commentForm.getC_id();
		/*
		 * 如果id为0则跳到登陆页
		 */
		if(c_id == 0){
			return mapping.findForward("login");
		}
		// 添加支持
		dao.addSupport(c_id);
		// 根据评论的id 取出支持的人数
		int n = dao.getSupport(c_id);
		// ajax发送
		AjaxUtil.send(response, n);
		return null;
	}
	
	/** 
	 * Method addAgainst 添加反对
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward null	
	 */
	public ActionForward addAgainst(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		CommentForm commentForm = (CommentForm) form; 
		// 得到点评的id 
		int c_id = commentForm.getC_id();
		/*
		 * 如果id为0则跳到登陆页
		 */
		if(c_id == 0){
			return mapping.findForward("login");
		}
		// 添加反对
		dao.addAgainst(c_id);
		// 根据评论的id 取出反对的人数
		int n = dao.getAgainst(c_id);
		// ajax发送
		AjaxUtil.send(response, n);
		return null;
	}
	
	/** 
	 * Method deleteCommet 删除点评
	 * @param mapping 配置文件
	 * @param form 表单数据
	 * @param request 网页请求
	 * @param response 响应
	 * @return ActionForward 查看所有点评	
	 */
	public ActionForward deleteComment(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		CommentForm commentForm = (CommentForm) form; 
		// 取出点评id
		int c_id = commentForm.getC_id();
		/*
		 * 否则就跳到登陆页
		 */
		if(c_id == 0){
			return mapping.findForward("login");
		}
		String doctorName = dao.getDoctorName(c_id);
		dao.deleteCommet(c_id);
		
		String sql = "comment where doctorName = '" + doctorName +"' order by c_id desc";
		// 调用分页类 
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		// 如果数据不为空 
		if(rs != null){
			// 封装
			List<CommentForm> comment = dao.getBean(rs);
			// 数据放到request中
			request.setAttribute("comments", comment);
		}
		request.setAttribute("message", "删除成功");
			return mapping.findForward("admin_allComment");
		 
	}
}